<?php
namespace app\common\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\Log;
use app\common\model\CarsInfo;
use app\common\model\OffAbnormal;
use app\common\model\OffNormal;
use app\common\model\OffRecord;

class OffLine extends Command
{
	protected function configure()
    {
        $this->setName('offLine')->setDescription('离线车辆11');
    }

    protected function execute(Input $input, Output $output)
    {   
        $cars = $this->offCars();

        if($cars){
            $data = $this->record();
            if(empty($data)){
                $data = [];
            }   
            $this->regular($cars,$data);
            $this->unusual($cars,$data);
            $this->normal($cars);
            $this->abnormal($cars);
        }
    	$output->writeln('success');
    }


    /*
        *cars_info表离线车辆 
    */
    public function offCars(){
        $time = time()*1000-3600000;
        try{

            $data = Db::name('cars_info')->where('network_status','<>',1)->where('machine_report_time','<',$time)->column('car_unique_id');

        }catch(\Exception $e){
            Log::record('查找离线1小时以上的车辆'.$e->getMessage(),'error');
            return false; 
        }

        return $data;
    }


    /*
        *白名单数据
    */
    public function record(){

        try{
            //白名单数据,即安全期内的车辆
            $cars = Db::name('off_record')->where('create_time','exp','>unix_timestamp()-`time`')->column('car_unique_id');

        }catch(\Exception $e){
            Log::record('查找白名单车辆数据'.$e->getMessage(),'error');
            return false;
        }
        return $cars;
    }


    /*
        *筛选出正常离线车辆
    */

    public function regular($param,$data){
        $cars = array_intersect($param, $data);  // ===>筛选出在白名单中,也在离线车辆中的车辆,即筛选出正常离线车辆
        if($cars){
            foreach ($cars as $key => $value) {
               $this->regenerate($value);    
            }
        }
    }

    /*
        *off_normal表插入更新数据
    */

    public function regenerate($value){
        $res = $this->renovate($value);
        if($res){
            $dataum = Db::name('off_normal')->where(['car_unique_id'=>$value])->find();
            $model = new OffNormal;
            if($dataum){
                try{

                    $model->allowField(true)->save($res,['car_unique_id'=>$value]);

                }catch(\Exception $e){
                    Log::record('更新off_normal表数据'.$e->getMessage(),'error');
                    return false;
                }
            }else{
                try{

                    $model->allowField(true)->save($res);

                }catch(\Exception $e){
                    Log::record('off_normal表插入数据'.$e->getMessage(),'error');
                }
            }
        }
    }

    /*
        *筛选异常离线车辆
    */

    public function unusual($param,$data){
        $data = array_diff($param,$data);    // ===>筛选出在离线车辆中,不在白名单中的车辆,即筛选出异常离线车辆
        if($data){
            foreach ($data as $key => $value) {
                $normal = Db::name('off_normal')->where(['car_unique_id'=>$value])->find();
                if($normal){
                    OffNormal::destroy(['car_unique_id'=>$value]);
                }
                $this->renewal($value);
            }
        }
    }


    /**
        *插入更新off_abnormal表
    */

    public function renewal($value){
        $res = $this->renovate($value);
        if($res){
            $dataum = Db::name('off_abnormal')->where(['car_unique_id'=>$value])->find();
            $model = new OffAbnormal;
            if($dataum){
                try{

                    $model->allowField(true)->save($res,['car_unique_id'=>$value]);

                }catch(\Exception $e){
                    Log::record('更新off_abnormal表数据'.$e->getMessage(),'error');
                    return false;
                }
            }else{
                try{

                    $model->allowField(true)->save($res);

                }catch(\Exception $e){
                    Log::record('off_abnormal表插入数据'.$e->getMessage(),'error');
                }
            }
        }
    }


    /*
        *正常离线表数据与离线车辆数据对比
    */
    public function normal($param){
        try{

            $cars = Db::name('off_normal')->column('car_unique_id');   //正常离线表车辆

        }catch(\Exception $e){
            Log::record('查找off_normal的车辆'.$e->getMessage(),'error');
            return []; 
        }
        $data = array_diff($cars,$param);    //  ===>在正常离线表中,不在离线车辆中,即筛选出已经在线的车辆
        if(!empty($data)){
            foreach ($data as $key => $value) {
                OffNormal::destroy(['car_unique_id'=>$value]);
            }
        }
    }

    /*
        *异常离线表数据与离线车辆对比
    */
    public function abnormal($param){
        try{

            $cars = Db::name('off_abnormal')->column('car_unique_id');   //异常离线表车辆

        }catch(\Exception $e){
            Log::record('查找off_abnormal的车辆'.$e->getMessage(),'error');
            return []; 
        }
        $data = array_diff($cars,$param);    //  ===>在异常离线表中,不在离线车辆中,即筛选出已经在线的车辆
        if(!empty($data)){
            foreach ($data as $key => $value) {
                OffAbnormal::destroy(['car_unique_id'=>$value]);
            }
        }
    }


    /*
        *cars表数据
    */

    public function renovate($value){
        $cars = Db::name('cars')->where(['car_unique_id'=>$value])->find();
        $cars_info = Db::name('cars_info')->where(['car_unique_id'=>$value])->field('machine_report_time')->find();
        if($cars){
            $dataum['car_sn'] = $cars['car_sn'];
            $dataum['car_unique_id'] = $value;
            $dataum['brand_id'] = $cars['brand_id'];
            $dataum['agency_id'] = $cars['agency_id'];
            $dataum['equipment_id'] = $cars['equipment_id'];
            $dataum['cardcode'] = $cars['cardcode'];
            $dataum['sim_id'] = $cars['sim_id'];
            $dataum['supplier_no'] = $cars['supplier_no'];
            $dataum['machine_report_time'] = $cars_info['machine_report_time']??'';
        }
        return $dataum;
    }
}











